package net.xju.dao;

import net.xju.common.utils.JdbcUtilsV1;
import net.xju.entity.Consultant;

import java.util.List;

public class ConsultantDao {
    public Consultant login(String username, String password) {
        String sql = "SELECT id, username, password, age, gender FROM Consultants WHERE username = ? AND password = ?";
        return JdbcUtilsV1.executeQueryOne(JdbcUtilsV1.getConnection(), Consultant.class, sql, username, password);
    }

    public List<Consultant> list() {
        String sql = "SELECT id, username, age, gender FROM Consultants";
        return JdbcUtilsV1.executeQuery(JdbcUtilsV1.getConnection(), Consultant.class, sql);
    }

    public boolean insert(Consultant consultant) {
        String sql = "INSERT INTO Consultants (username, password, age, gender) VALUES (?,?,?,?)";
        int i = JdbcUtilsV1.executeUpdate(sql, consultant.getUsername(), consultant.getPassword(), consultant.getAge(), consultant.getGender());
        return i > 0;
    }

    public boolean update(Consultant consultant) {
        String sql = "UPDATE Consultants SET username = ?, password = ?, age = ?, gender = ? WHERE id = ?";
        int i = JdbcUtilsV1.executeUpdate(sql, consultant.getUsername(), consultant.getPassword(), consultant.getAge(), consultant.getGender(), consultant.getId());
        return i > 0;
    }

    public boolean delete(int id) {
        String sql = "DELETE FROM Consultants WHERE id = ?";
        int i = JdbcUtilsV1.executeUpdate(sql, id);
        return i > 0;
    }
}